﻿using System;
using System.Diagnostics;
using Utils.Wpf;

namespace StockAnalyst2.StockGrid
{
	[DebuggerDisplay("Visible = {Visible}, DisplayIndex = {DisplayIndex}, NotifyChange = {NotifyChange}")]
	internal class ColumnOptionsVM : ViewModelValidatedBase, IDisposable
	{
		#region Properties

		public ColumnOptions Model { get; private set; }

		public bool Visible
		{
			get
			{
				return this.Model.Visible;
			}
			set
			{
				if (this.Model.Visible != value)
				{
					this.Model.Visible = value;
					base.OnPropertyChanged("Visible");
				}
			}
		}

		public int DisplayIndex
		{
			get
			{
				return this.Model.DisplayIndex;
			}
			set
			{
				if (this.Model.DisplayIndex != value)
				{
					this.Model.DisplayIndex = value;
					base.OnPropertyChanged("DisplayIndex");
				}
			}
		}

		public bool NotifyChange
		{
			get
			{
				return this.Model.NotifyChange;
			}
			set
			{
				if (this.Model.NotifyChange != value)
				{
					this.Model.NotifyChange = value;
					base.OnPropertyChanged("NotifyChange");
				}
			}
		}

		public bool DisplayArrow
		{
			get
			{
				return this.Model.DisplayArrow;
			}
			set
			{
				if (this.Model.DisplayArrow != value)
				{
					this.Model.DisplayArrow = value;
					base.OnPropertyChanged("DisplayArrow");
				}
			}
		}

		public bool ColorValue
		{
			get
			{
				return this.Model.ColorValue;
			}
			set
			{
				if (this.Model.ColorValue != value)
				{
					this.Model.ColorValue = value;
					base.OnPropertyChanged("ColorValue");
				}
			}
		}

		public decimal Limit
		{
			get
			{
				return this.Model.Limit;
			}
			set
			{
				if (this.Model.Limit != value)
				{
					this.Model.Limit = value;
					base.OnPropertyChanged("Limit");
				}
			}

		}
		#endregion

		#region Events

		public event EventHandler<ValidationRequestedEventArgs> ValidationRequested;

		#endregion

		#region Public Methods

		public ColumnOptionsVM(ColumnOptions model)
		{
			this.Model = model;
			this.Model.PropertyChanged += this.Model_PropertyChanged; // Disposed
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "DisplayIndex")
			{
				this.OnValidationRequired(out result);
			}

			return result;
		}

		protected virtual void OnValidationRequired(out string validationResult)
		{
			if (this.ValidationRequested != null)
			{
				ValidationRequestedEventArgs e = new ValidationRequestedEventArgs();
				this.ValidationRequested(this, e);
				validationResult = e.ValidationResult;
			}
			else
			{
				validationResult = null;
			}
		}

		#endregion

		#region Event Handlers

		private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e.PropertyName);
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				this.Model.PropertyChanged -= this.Model_PropertyChanged;
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~ColumnOptionsVM()
		{
			this.Dispose(false);
		}

		#endregion
	}
}
